{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "35dc2507",
   "metadata": {},
   "source": [
    "# 本周知识\n",
    "##  1.函数的返回值\n",
    "## 2.函数的输入参数类型\n",
    "## 3.函数的返回参数类型\n",
    "## 4.模块的概念\n",
    "## 5.如何创建一个模块，并且调用自己定义的模块"
   ]
  },
  {
   "cell_type": "raw",
   "id": "ac79435e",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "09fdfcee",
   "metadata": {},
   "source": [
    "## 1.函数的返回值\n",
    "* 1. 返回值关键字；return\n",
    "> A. 函数一定有返回值（无论是什么编程语言）   \n",
    "> B. 在其他的编程语言中（如c语言，java等），返回值一定要写，不写会报错，对于python（更高级的语言），返回值如果不写，给定默认值 None   \n",
    "> C. 返回值一般为函数体运行的结果（函数返回值实践1），也可以是自定义的函数返回值（实践2）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5326f5f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践1\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "#     print(full_name)\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "398f2d0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Li Hua'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_formatted_name(\"Li\",\"Hua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5580ca08",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践2\n",
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == \"Lihua\" and password == 123456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7acab69a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"Lihua\",123456)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d85ccad5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"Lihua\",12345678)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "176ea56f",
   "metadata": {},
   "source": [
    "## 2.函数的输入参数类型\n",
    "* 1. 函数可以输入复杂的参数\n",
    "> A. 函数参数为列表   \n",
    "> B. 函数参数为字典  \n",
    "> C. ...\n",
    "\n",
    "* 2. 函数可以输入不确定个数的参数\n",
    "> A. *args   \n",
    "> B. ** kwargs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62c18ef4",
   "metadata": {},
   "source": [
    "* 1. 函数可以输入复杂的参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a97cb750",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_A(names):\n",
    "    \"\"\"测试函数参数输入为列表\"\"\"\n",
    "    return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "43b88347",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Lihua', 'Alex', 'Ziqi']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_A([\"Lihua\",\"Alex\",\"Ziqi\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "07dd498d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_B(person):\n",
    "    \"\"\"测试函数参数输入为字典\"\"\"\n",
    "    return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "37984d9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu_id': 1001, 'age': 18, 'job': 'student'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_B({\"stu_id\":1001,\"age\":18,\"job\":\"student\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c38cfc46",
   "metadata": {},
   "source": [
    "* 2. 函数可以输入不确定个数的参数\n",
    "\n",
    "* 1. *args\n",
    "> A. args 可以接受多个数据（单值）  \n",
    "> B. args 将多个数据存在一个**元组**当中，实际还是一个变量  \n",
    "\n",
    "* 2. ** kwargs\n",
    "> A. kwargs 可以接受多个数据（键值对） ==> 关键字参数   \n",
    "> B. kwargs 将多个键值对的数据存放在一个**字典**中，实际上还是一个变量   \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "6e789f88",
   "metadata": {},
   "outputs": [],
   "source": [
    "def food_ingred(food,*ingreds):\n",
    "    \"\"\"食物配料单\"\"\"\n",
    "    return(food,ingreds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "d258cb4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers', 'extra cheese'))"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "food_ingred(\"pizza\",\"mushroom\", \"green peppers\", \"extra cheese\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "1d568a86",
   "metadata": {},
   "outputs": [],
   "source": [
    "def person(stu_name,**stu_course):\n",
    "    \"\"\"学生个人信息完善表\"\"\"\n",
    "    return stu_name,stu_course"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "63d2829e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Lihua', {'course_Mon': 'API', 'course_Tue': 'app原型'})"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person(\"Lihua\",course_Mon = \"API\", course_Tue = \"app原型\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15a6b6f6",
   "metadata": {},
   "source": [
    "## 3.函数的返回参数类型\n",
    "* 1. 在编程语言中，函数的返回值只能有一个值\n",
    "* 2. 返回值可以是 字符串、数值、列表、字典等数据（单一值）\n",
    "* 3. return 后面写多个值，将会返回一个元组（一个值，不违背函数在编程思维中的定义）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "278ea868",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值return为字符串\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9dc5606c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "698f5a66",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return为列表\"\"\"\n",
    "    return [\"Lihua\", \"Alex\", \"Ziqi\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "50e092f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Lihua', 'Alex', 'Ziqi']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8aa0ea3",
   "metadata": {},
   "source": [
    "* return 后面写多个值，将会返回一个元组（一个值，不违背函数在编程思维中的定义）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "2e49b322",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return为多个值的情况\"\"\"\n",
    "    return 18, \"Lihua\", [\"Lihua\", \"Alex\", \"Ziqi\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "ffc23d0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(18, 'Lihua', ['Lihua', 'Alex', 'Ziqi'])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "fe1d4977",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值return为多个值的情况\"\"\"\n",
    "    return 18,13,15,17,23"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "0edd3ed1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(18, 13, 15, 17, 23)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b7eef20",
   "metadata": {},
   "source": [
    "## 4.模块的概念\n",
    "* 1. 使用模块可以隐藏函数的源代码\n",
    "* 2. 使用的人不需要会底层代码（只需要会使用模块即可）\n",
    "* 3. 模块的使用的方法：\n",
    "> A. import模块名，使用：模块名.函数名()  \n",
    "> B. from模块名import函数名，使用：函数名()  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "bf2be641",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "2957c3a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=9, tm_hour=11, tm_min=43, tm_sec=14, tm_wday=3, tm_yday=343, tm_isdst=0)"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7d7f863",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "c7aff303",
   "metadata": {},
   "outputs": [],
   "source": [
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "7f8771eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o'}"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 函数的调用\n",
    "search4vowels(\"hello xiaoma\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "5d792a17",
   "metadata": {},
   "source": [
    "pypi.org"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf8c4f36",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ab8d539",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
